package com.hyb.hybmodulesblog.controller;

import java.util.Date;
import java.util.List;
import java.io.IOException;
import javax.servlet.http.HttpServletResponse;

import com.hyb.hybmodulesblog.domain.Tribe;
import com.hyb.hybmodulesblog.domain.TribeUser;
import com.hyb.hybmodulesblog.service.ITribeService;
import com.hyb.hybmodulesblog.service.ITribeUserService;
import com.ruoyi.common.core.constant.SecurityConstants;
import com.ruoyi.common.core.domain.R;
import com.ruoyi.common.core.utils.DateUtils;
import com.ruoyi.common.security.utils.SecurityUtils;
import com.ruoyi.system.api.RemoteSchoolRoleService;
import com.ruoyi.system.api.domain.SchoolRole;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import com.ruoyi.common.log.annotation.Log;
import com.ruoyi.common.log.enums.BusinessType;
import com.ruoyi.common.security.annotation.RequiresPermissions;
import com.hyb.hybmodulesblog.domain.ApproveTribeUser;
import com.hyb.hybmodulesblog.service.IApproveTribeUserService;
import com.ruoyi.common.core.web.controller.BaseController;
import com.ruoyi.common.core.web.domain.AjaxResult;
import com.ruoyi.common.core.utils.poi.ExcelUtil;
import com.ruoyi.common.core.web.page.TableDataInfo;

/**
 * 用户申请加入部落表的增删改查Controller
 * 
 * @author hyb
 * @date 2023-03-11
 */
@RestController
@RequestMapping("/approveTribeUser")
public class ApproveTribeUserController extends BaseController
{
    @Autowired
    private IApproveTribeUserService approveTribeUserService;

    @Autowired
    ITribeUserService tribeUserService;

    @Autowired
    ITribeService tribeService;

    @Autowired
    RemoteSchoolRoleService schoolRoleService;

    /*
    * 申请加入部落
    * */

    @PostMapping("/addUser")
    public AjaxResult addUser(@RequestParam("tribeId")Long tribeId){
        Long userId = SecurityUtils.getUserId();
        TribeUser tribeUser = new TribeUser();
        tribeUser.setTribeId(tribeId);
        tribeUser.setUserId(userId);
        //先看是不是已经加入
        List<TribeUser> tribeUsers = tribeUserService.selectTribeUserList(tribeUser);
        if (tribeUsers.size()!=0){
            return AjaxResult.error("您已加入,不能再次发送申请");
        }
        //查看部落是否存在
        Tribe tribe = tribeService.selectTribeById(tribeUser.getTribeId());
        if (tribe==null){
            return AjaxResult.error("部落不存在");
        }
        //查看是否已经存在申请
        ApproveTribeUser approveTribeUser1 = new ApproveTribeUser();
        approveTribeUser1.setPf1(String.valueOf(tribeId));
        approveTribeUser1.setToApproveUserId(userId);
        List<ApproveTribeUser> approveTribeUsers = approveTribeUserService.selectApproveTribeUserList(approveTribeUser1);
        if (approveTribeUsers.size()!=0){
            return AjaxResult.error("已经申请,请耐心等待回应哦");
        }

        //将申请用户查出来

        R<SchoolRole> role =
                schoolRoleService.getPersonalSchoolRole(userId, SecurityConstants.INNER);
        SchoolRole data = role.getData();
        ApproveTribeUser approveTribeUser = new ApproveTribeUser();
        approveTribeUser.setToApproveUserId(userId);
        approveTribeUser.setToApproveNumber(data.getNumber());
        approveTribeUser.setToApproveRealName(data.getName());
        Date nowDate = DateUtils.getNowDate();
        approveTribeUser.setToApproveTime(nowDate);
        approveTribeUser.setCreateTime(nowDate);
        approveTribeUser.setUpdateTime(nowDate);
        approveTribeUser.setPf1(String.valueOf(tribe.getId()));
        approveTribeUser.setPf2(tribe.getNumber());
        approveTribeUser.setPf3(tribe.getName());
        approveTribeUserService.insertApproveTribeUser(approveTribeUser);
        return AjaxResult.success();
    }

    /*
    * 审核这个申请
    * */
    @RequiresPermissions("approveTribeUser:approveTribeUser:addUser")
    @PostMapping("/approveUser")
    public AjaxResult approveUser(
            @RequestParam("id")Long id,
            @RequestParam("status")Long status,
            @RequestParam("remark")String remark

    ){
        ApproveTribeUser approveTribeUser = new ApproveTribeUser();
        approveTribeUser.setId(id);
        approveTribeUser.setStatus(status);
        approveTribeUser.setPf4(remark);
        Long userId=SecurityUtils.getUserId();
        R<SchoolRole> personalSchoolRole = schoolRoleService.getPersonalSchoolRole(userId, SecurityConstants.INNER);
        SchoolRole data = personalSchoolRole.getData();
        approveTribeUser.setApproveUserId(data.getUserId());
        approveTribeUser.setApproveNumber(data.getNumber());
        approveTribeUser.setApproveRealName(data.getName());
        Date nowDate = DateUtils.getNowDate();
        approveTribeUser.setApproveTime(nowDate);
        approveTribeUser.setUpdateTime(nowDate);
        approveTribeUserService.updateApproveTribeUser(approveTribeUser);

        //向关联表插入一条数据
        TribeUser tribeUser = new TribeUser();
        approveTribeUser=approveTribeUserService.selectApproveTribeUserById(approveTribeUser.getId());
        tribeUser.setUserId(userId);
        tribeUser.setTribeId(Long.parseLong(approveTribeUser.getPf1()));
        tribeUserService.insertTribeUser(tribeUser);

        return AjaxResult.success();
    }


    /**
     * 查询用户申请加入部落表的增删改查列表
     */
    @RequiresPermissions("approveTribeUser:approveTribeUser:list")
    @GetMapping("/list")
    public TableDataInfo list(ApproveTribeUser approveTribeUser)
    {
        startPage();
        List<ApproveTribeUser> list = approveTribeUserService.selectApproveTribeUserList(approveTribeUser);
        return getDataTable(list);
    }

    /**
     * 导出用户申请加入部落表的增删改查列表
     */
    @RequiresPermissions("approveTribeUser:approveTribeUser:export")
    @Log(title = "用户申请加入部落表的增删改查", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, ApproveTribeUser approveTribeUser)
    {
        List<ApproveTribeUser> list = approveTribeUserService.selectApproveTribeUserList(approveTribeUser);
        ExcelUtil<ApproveTribeUser> util = new ExcelUtil<ApproveTribeUser>(ApproveTribeUser.class);
        util.exportExcel(response, list, "用户申请加入部落表的增删改查数据");
    }

    /**
     * 获取用户申请加入部落表的增删改查详细信息
     */
    @RequiresPermissions("approveTribeUser:approveTribeUser:query")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return success(approveTribeUserService.selectApproveTribeUserById(id));
    }

    /**
     * 新增用户申请加入部落表的增删改查
     */
    @RequiresPermissions("approveTribeUser:approveTribeUser:add")
    @Log(title = "用户申请加入部落表的增删改查", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody ApproveTribeUser approveTribeUser)
    {
        return toAjax(approveTribeUserService.insertApproveTribeUser(approveTribeUser));
    }

    /**
     * 修改用户申请加入部落表的增删改查
     */
    @RequiresPermissions("approveTribeUser:approveTribeUser:edit")
    @Log(title = "用户申请加入部落表的增删改查", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody ApproveTribeUser approveTribeUser)
    {
        return toAjax(approveTribeUserService.updateApproveTribeUser(approveTribeUser));
    }

    /**
     * 删除用户申请加入部落表的增删改查
     */
    @RequiresPermissions("approveTribeUser:approveTribeUser:remove")
    @Log(title = "用户申请加入部落表的增删改查", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(approveTribeUserService.deleteApproveTribeUserByIds(ids));
    }
}
